package com.iretailer.report.influxdb.service;

import com.iretailer.report.influxdb.dao.InfluxdbDao;
import com.iretailer.report.influxdb.model.QueueLength;
import com.iretailer.report.influxdb.util.TableName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/3/11 13:38
 * @Description:
 */
@Service
public class InfluxdbQueueService {
    @Autowired
    private InfluxdbDao influxdbDao;

    public int insertQueueLength(List<QueueLength> post, TableName.Queue table) {
        int result = -1;
        int separateNumber = 50000;
        int size = post.size();
        if (size <= separateNumber) {
            result = influxdbDao.batchInsertQueueLength(post, table);
        } else {
            int batchSize = size / separateNumber;
            int remain = size % separateNumber;
            for (int i = 0; i < batchSize; ) {
                int index = i * separateNumber;
                int toIndex = index + separateNumber;
                result += influxdbDao.batchInsertQueueLength(post.subList(index, toIndex), table);
            }
            if (remain > 0) {
                result += influxdbDao.batchInsertQueueLength(post.subList(size - remain, size), table);
            }

        }
        return result;
    }
}
